<?php
namespace WebAdmin\Controller;
use Think\Controller;
class BaseController extends Controller {

    //ThinkPHP的构造函数
    public function _initialize() {
        //检查是否登录
        $this->check_login();
        C('defaultinfo.name','后台管理中心');
    }

    //检查是否登录
    public function check_login(){
        //判断是否登录以及是否管理员帐号，如果不是则跳转到登录页面
        if($_SESSION['is_admin']!=1 && CONTROLLER_NAME!="Login"){
            echo "<script>window.location.href='".__APP__."/index.php/Login'</script>";exit;
        }
    }
    
    public function getData(){
    	$DataModel = D(CONTROLLER_NAME);
    	$items = array();
    	$page = (int)$_GET['page'];
    	$items['page'] = $page>0 ? $page :1;
    	$items['records'] = $DataModel->count();
    	$limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
    	$items['total'] = ceil($items['records']/$limit);
    	$list = $DataModel->page($page)->limit($limit)->select();
    	$items['rows'] = $list;
    	$this->ajaxReturn($items);
    }
    
    public function delete(){
    	if(!empty($_POST)) {
    		$DataModel = D(CONTROLLER_NAME);
    		$id_arr = explode(",", $_POST['ids']);
    		foreach($id_arr as $id){
    			$result = $DataModel->delete((int)$id);
    		}
    		if($result){
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}
    }

}